package com.example.add8_17.fragment;


import android.content.Intent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.add8_17.R;
import com.example.add8_17.WebActivity;
import com.example.add8_17.adapter.Homeadapter;
import com.example.add8_17.base.BaseFragment;
import com.example.add8_17.bean.HomeBean;
import com.example.add8_17.presenter.HomePresenter;
import com.example.add8_17.sql.Sql;
import com.example.add8_17.view.HomeView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseFragment<HomePresenter> implements HomeView {
    @BindView(R.id.home_re)
    RecyclerView mhome_re;
    @BindView(R.id.smart)
    SmartRefreshLayout msmart;
    private ArrayList<HomeBean.DataBean.DatasBean> datasBeans;
    private Homeadapter homeadapter;
    private int page=0;
    private int pos;
    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected HomePresenter initPresenter() {
        return mPresenter=new HomePresenter();
    }

    @Override
    protected void initView(View mView) {
        mPresenter.getdata(page);
        setHasOptionsMenu(true);
        registerForContextMenu(mhome_re);
        mhome_re.setLayoutManager(new LinearLayoutManager(getContext()));
        mhome_re.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayout.VERTICAL));
        datasBeans = new ArrayList<>();
        homeadapter = new Homeadapter(getContext(), datasBeans);
        mhome_re.setAdapter(homeadapter);

        homeadapter.setContextClickListener(new Homeadapter.OnContextClickListener() {
            @Override
            public void oncontextclicklistener(View v, int position) {
                pos=position;
            }
        });
        homeadapter.setOncontextclicklistener1(new Homeadapter.oncontextclick() {
            @Override
            public void onlicklistener(View v, int position) {
                String link = datasBeans.get(position).getLink();
                Intent intent = new Intent(getContext(), WebActivity.class);
                intent.putExtra("link",link);
                startActivity(intent);
            }
        });
        msmart.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page=0;
                datasBeans.clear();
                mPresenter.getdata(page);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page++;
                mPresenter.getdata(page);
                msmart.finishRefresh();
            }
        });
    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        inflater.inflate(R.menu.layout,menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.g:
                mhome_re.setLayoutManager(new GridLayoutManager(getContext(),3));
                homeadapter.notifyDataSetChanged();
                break;
            case R.id.line1:
                mhome_re.setLayoutManager(new LinearLayoutManager(getContext()));
                homeadapter.notifyDataSetChanged();
                break;
            case R.id.s:
                mhome_re.setLayoutManager(new StaggeredGridLayoutManager(2,LinearLayout.VERTICAL));
                homeadapter.notifyDataSetChanged();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCreateContextMenu(@NonNull ContextMenu menu, @NonNull View v, @Nullable ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0,0,0,"收藏");
        menu.add(1,1,1,"删除");
    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case 0:
                Sql sql = new Sql(getContext());
               sql.nameinsert(datasBeans.get(pos).getTitle());
                Toast.makeText(getContext(),"收藏成功",Toast.LENGTH_LONG).show();
                break;
            case 1:
                datasBeans.remove(pos);
                homeadapter.notifyDataSetChanged();
                Toast.makeText(getContext(),"删除成功",Toast.LENGTH_LONG).show();
                break;
        }
        return super.onContextItemSelected(item);
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_home;
    }

    @Override
    public void onSuccess(HomeBean homedata) {
        if (homedata!=null){
            datasBeans.addAll(homedata.getData().getDatas());
            homeadapter.notifyDataSetChanged();
            msmart.finishRefresh();
        }else{
            Toast.makeText(getContext(),"没有数据",Toast.LENGTH_LONG).show();
            msmart.finishRefresh();
        }
    }

    @Override
    public void onFali(String error) {

    }

    @Override
    public void showToast(String str) {

    }
}
